package com.djl.service.impl;

import com.djl.crowd.entity.Role;
import com.djl.crowd.entity.RoleExample;
import com.djl.mapper.RoleMapper;
import com.djl.service.RoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author le
 * @Date 2021/11/20 15:21
 * @Version 1.0
 */
@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;


    @Override
    public List<Role> getAssignedRole(Integer adminId) {
        return roleMapper.selectAssignedRole(adminId);
    }

    @Override
    public List<Role> getUnAssignRole(Integer adminId) {
        return roleMapper.selectUnAssignedRole(adminId);
    }

    @Override
    public int removeRole(List<Integer> roles) {
        RoleExample example = new RoleExample();
        example.createCriteria().andIdIn(roles);
        return roleMapper.deleteByExample(example);
    }

    @Override
    public int updateRole(Role role) {
        return roleMapper.updateByPrimaryKey(role);
    }

    @Override
    public int saveRole(Role role) {
        return roleMapper.insert(role);
    }

    @Override
    public PageInfo<Role> getPageInfo(Integer pageNum, Integer pageSize, String keyword) {
        // 开启分页
        PageHelper.startPage(pageNum, pageSize);
        // 查询
        List<Role> roles = roleMapper.selectRoleByKeyword(keyword);
        // 封装为分页对象返回
        return new PageInfo<>(roles);
    }
}
